package com.xzx.shop_front_api.controller;

import com.xzx.shop_front_api.entity.ESNews;
import com.xzx.shop_front_api.entity.Test;
import com.xzx.shop_front_api.utils.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.client.elc.NativeQuery;
import org.springframework.data.elasticsearch.core.ElasticsearchOperations;
import org.springframework.data.elasticsearch.core.IndexOperations;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.data.elasticsearch.core.query.HighlightQuery;
import org.springframework.data.elasticsearch.core.query.highlight.Highlight;
import org.springframework.data.elasticsearch.core.query.highlight.HighlightField;
import org.springframework.data.elasticsearch.core.query.highlight.HighlightParameters;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("api/test")
public class TestController {
    @Autowired
    private ElasticsearchOperations elasticsearchOperations;
    @GetMapping("create")
    public JsonResult create(){
        IndexOperations indexOperations = elasticsearchOperations.indexOps(Test.class);
        if(indexOperations.exists()){
            indexOperations.create();
        }
        return JsonResult.success("OK",null);
    }

    // 索引文档
    @PostMapping
    public JsonResult add(@RequestBody Test test){
        System.out.println(test);
        Test result = elasticsearchOperations.save(test);
        return JsonResult.success("添加成功",test);
    }

    // 删除文档
    @GetMapping("delete")
    public JsonResult delete(@RequestParam("id") String id){
        if (id.isEmpty()){
            return JsonResult.failed("id不能为空");
        }
        String result = elasticsearchOperations.delete(id, Test.class);
        return JsonResult.success("删除成功",null);
    }

    // ik分词搜索
    @GetMapping("match")
    public JsonResult update(@RequestParam("wd") String wd) {
        NativeQuery query = NativeQuery.builder()
                .withQuery(q -> q
                        .match(m -> m
                                .field("title")
                                .query(wd)
                        )
                )
                .build();
        SearchHits<Test> searchHits = elasticsearchOperations.search(query, Test.class);
        HashMap<String,Object> result = new HashMap<>();
        result.put("hits",searchHits.getSearchHits());
        result.put("totalHits",searchHits.getTotalHits());
        return JsonResult.success("搜索成功",result);
    }

    // 高亮搜索
    @GetMapping("higlight")
    public JsonResult higlight(@RequestParam("wd") String wd){
        // 创建高亮配置
        HighlightField highlightField = new HighlightField("title");
        HighlightParameters parameters = HighlightParameters.builder()
                .withPreTags("<span style='color: red'>")
                .withPostTags("</span>")
                .withFragmentSize(Integer.MAX_VALUE)
                .withNumberOfFragments(1)
                .build();
        Highlight highlight = new Highlight(parameters, List.of(highlightField));
        HighlightQuery highlightQuery = new HighlightQuery(highlight, Test.class);


        // 构建查询
        NativeQuery query = NativeQuery.builder()
                .withQuery(q -> q
                        .match(m -> m
                                .field("title")
                                .query(wd)
                        )
                )
                .withHighlightQuery(highlightQuery)
                .build();
        SearchHits<Test> searchHits = elasticsearchOperations.search(query, Test.class);
        HashMap<String, Object> resultData = new HashMap<>();
        resultData.put("hits", searchHits.getSearchHits());
        resultData.put("totalHits", searchHits.getTotalHits());
        return JsonResult.success("搜索成功", resultData);
    }
}
